package ferp.android.dialogs;

import android.content.Context;
import ferp.android.R;
import ferp.android.managers.ProfileManager;
import ferp.core.game.Game;
import ferp.core.game.Input;
import ferp.core.player.Profile;

/**
 * User: olegs
 * Date: 4/10/14 12:11 PM
 */
public class OfferDialog extends ObjectRadioListViewDialog<Input.Offer>
{
  public interface Listener extends ObjectRadioListViewDialog.Listener<Input.Offer> {}

  public static OfferDialog create(Context context, Listener listener)
  {
    Profile profile = ProfileManager.instance(context).getCurrentProfile();

    if (profile == null)
      return null;

    Game game = profile.game();

    return game != null && game.options.isOfferSet() ? new OfferDialog(context, listener) : null;
  }

  public void prepare()
  {
    Context context = getContext();
    Game g = ProfileManager.instance(context).getCurrentProfile().game();
    Input.Options io = g.options;

    // clear 'options' and 'offers' lists
    clear();

    // fill 'options' and 'offers' lists
    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT_PLUS_3))
      add(String.valueOf(g.contract() + 3), Input.Offer.TRICKS_BY_CONTRACT_PLUS_3, true);

    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT_PLUS_2))
      add(String.valueOf(g.contract() + 2), Input.Offer.TRICKS_BY_CONTRACT_PLUS_2, true);

    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT_PLUS_1))
      add(String.valueOf(g.contract() + 1), Input.Offer.TRICKS_BY_CONTRACT_PLUS_1, true);

    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT))
      add(context.getString(R.string.offer_tricks_by_contract) + " (" + g.contract() + ")", Input.Offer.TRICKS_BY_CONTRACT, true);

    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT_MINUS_1))
      add(context.getString(R.string.offer_tricks_without_1), Input.Offer.TRICKS_BY_CONTRACT_MINUS_1, true);

    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT_MINUS_2))
      add(context.getString(R.string.offer_tricks_without_2), Input.Offer.TRICKS_BY_CONTRACT_MINUS_2, true);

    if (io.isSet(Input.Options.TRICKS_BY_CONTRACT_MINUS_3))
      add(context.getString(R.string.offer_tricks_without_3), Input.Offer.TRICKS_BY_CONTRACT_MINUS_3, true);

    if (io.isSet(Input.Options.NO_MORE_TRICKS))
      add(context.getString(R.string.offer_no_more_tricks), Input.Offer.NO_MORE_TRICKS, true);

    if (io.isSet(Input.Options.REST_TRICKS_ARE_MINE))
      add(context.getString(R.string.offer_rest_tricks_are_mine), Input.Offer.REST_TRICKS_ARE_MINE, true);

    ld.invalidate();
  }

  private OfferDialog(Context context, Listener listener)
  {
    super(context, listener, R.string.offer);
  }
}
